package com.boding.power.controller;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.belerweb.social.bean.Result;
import com.belerweb.social.qq.connect.api.QQConnect;
import com.belerweb.social.qq.connect.api.User;
import com.belerweb.social.qq.connect.bean.AccessToken;
import com.belerweb.social.qq.connect.bean.OpenID;
import com.boding.power.common.Constants;
import com.boding.power.model.Businessinfo;
import com.boding.power.model.Customer;
import com.boding.power.model.News;
import com.boding.power.model.UserRoles;
import com.boding.power.model.Users;
import com.boding.power.model.Vipinfo;
import com.boding.power.utils.CryptographyUtil;
import com.boding.power.utils.QiNiuUtil;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.render.CaptchaRender;
import com.jfinal.upload.UploadFile;
import com.qiniu.util.Auth;

/**
 * 前台页面相关的操作
 * 
 * @author giegie
 * 
 */
public class PageController extends Controller {
	@ActionKey("/")
	public void index() {
		setAttr("a", News.dao.findByType("新闻"));
		setAttr("b", News.dao.findByType("技术"));
		setAttr("c", News.dao.findByType("文章"));
		setAttr("d", News.dao.findByType("视频"));
		setAttr("e", News.dao.findByType("展会"));
		render("/index.jsp");
	}

	@ActionKey("/img")
	public void img() {
		CaptchaRender img = new CaptchaRender();
		render(img);
	}

	public void regUI() {
		render("reg.jsp");
	}

	public void reg() {
		String[] passwords = getParaValues("password");
		Users u = getModel(Users.class);
		String username = u.getUsername();
		boolean loginSuccess = CaptchaRender.validate(this, getPara("code"));
		if (loginSuccess && u != null && StringUtils.isNotEmpty(username)
				&& StringUtils.isNotBlank(username)) {
			Users user = u.findByUsername(username);// 判断账号唯一
			if (user == null && passwords != null && passwords.length == 2
					&& passwords[0].equals(passwords[1])) {// 判断密码
				u.setPassword(CryptographyUtil
						.md5(passwords[0], Constants.SALT));
				u.setStatus(1); // 设置状态正常
				u.setCreateTime(new Date());
				boolean result = u.save();
				if (result) {// 给用户赋予权限
					Users users = u.findByUsername(username);
					UserRoles userRoles = new UserRoles();
					userRoles.setUserId(users.getId());
					userRoles.setCreateTime(new Date());
					switch (u.getType()) { // 根据用户类型储存,跳转到相关的完善信息页面
					case 2: {
						userRoles.setRoleId(Constants.CUSTOMER);
						userRoles.save();
						redirect("/front/cusPerfectInfoUI?user_id="
								+ users.getId() + "&username="
								+ users.getUsername());
						break;
					}
					case 3: {
						userRoles.setRoleId(Constants.BUSSINESS);
						userRoles.save();
						redirect("/front/busPerfectInfoUI?user_id="
								+ users.getId() + "&username="
								+ users.getUsername());
						break;
					}
					case 4: {
						userRoles.setRoleId(Constants.VIP);
						userRoles.save();
						redirect("/front/vipPerfectInfoUI?user_id="
								+ users.getId() + "&username="
								+ users.getUsername());
						break;
					}
					}
				} else {
					setAttr("error", "注册失败");
					render("reg.jsp");
				}
			} else {
				setAttr("error", "用户名已存在");
				render("reg.jsp");
			}
		} else {
			setAttr("error", "验证码错误");
			render("reg.jsp");
		}
	}

	/**
	 * 普通用户完善信息
	 */
	public void cusPerfectInfoUI() {
		setAttr("user_id", getPara("user_id"));
		setAttr("username", getPara("username"));
		render("customerInfo.jsp");
	}

	public void cusPerfectInfo() {
		Customer customer = getModel(Customer.class);
		customer.setCreateDate(new Date());
		customer.save();
		renderText("发动之家主页");
	}

	/**
	 * 普通商户完善信息
	 */
	public void busPerfectInfoUI() {
		setAttr("user_id", getPara("user_id"));
		setAttr("username", getPara("username"));
		render("busInfo.jsp");
	}

	public void busPerfectInfo() {
		UploadFile f = getFile();
		Businessinfo businessinfo = getModel(Businessinfo.class);
		if (f != null) {
			File file = f.getFile();
			String key = QiNiuUtil.uploadFile(file);
			file.delete();
			businessinfo.setImg(key);
		}
		businessinfo.setCreateDate(new Date());
		businessinfo.save();
		redirect("/login");
	}

	/**
	 * VIP商户完善信息
	 */
	public void vipPerfectInfoUI() {
		setAttr("user_id", getPara("user_id"));
		setAttr("username", getPara("username"));
		render("vipInfo.jsp");
	}

	public void vipPerfectInfo() {
		Vipinfo vipinfo = getModel(Vipinfo.class);
		vipinfo.setCreateDate(new Date());
		vipinfo.save();
		redirect("/login");
	}
}
